package com.edu.hre.struts.action.leave;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreLeaveRemain;
import com.edu.hre.db.pojo.HreLeaveRequest;
import com.edu.hre.db.pojo.HreLeaveType;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.leave.LeaveApproveUpdateForm;
import com.edu.hre.struts.form.leave.LeaveRequestUpdateForm;
import com.edu.hre.struts.form.leave.LeaveSummaryUpdateForm;

public class LeaveSummaryUpdateAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(LeaveSummaryUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		LeaveSummaryUpdateForm beanForm = (LeaveSummaryUpdateForm) form;
		HreUser user = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		
		int id = Integer.parseInt(request.getParameter("id"));
		HreLeaveRequest leaveRequest = baseService.getPojo(id, HreLeaveRequest.class);
		if(request.getParameter("btnEdit") == null){
			HreEmployee employee = baseService.getPojo(leaveRequest.getEmpNum(), HreEmployee.class);
			HreLeaveType leaveType = baseService.getPojo(leaveRequest.getLeaveTypeId(), HreLeaveType.class);
			beanForm.setLeaveTypeId(leaveType.getId());
			beanForm.setApproved(leaveRequest.getApproved());
			beanForm.setEmpFullName(CommonUtils.getFullName(employee));
			beanForm.setFirstLeave(leaveRequest.getFirstLeave());
			beanForm.setLastLeave(leaveRequest.getLastLeave());
			beanForm.setUser(employee.getMainUsername());
			beanForm.setLeaveTypeName(leaveType.getName()); 
			beanForm.setSupComment(leaveRequest.getSupComment());
			beanForm.setEmpNum(leaveRequest.getEmpNum());
			beanForm.setTotalLeaved(leaveRequest.getTotalLeaved());
			beanForm.setTotalLeavedStr(beanForm.getTotalLeavedThisTime());
			beanForm.setMessage("");
			return mapping.findForward("leaveSummaryUpdate");
		} 
		float totalLeavedEntered = CommonUtils.parseFloat(beanForm.getTotalLeavedStr(), 0f);
		if(totalLeavedEntered < 0 
				|| totalLeavedEntered > LeaveRequestUpdateAction.computeLeaveDays(leaveRequest.getFirstLeave(), leaveRequest.getLastLeave())
				|| (int)(totalLeavedEntered/0.5) != totalLeavedEntered/0.5 ){
			beanForm.setMessage("Invalid entered leaved day(s), please try later!");
			return mapping.findForward("leaveSummaryUpdate");
		}
		leaveRequest.setTotalLeaved(totalLeavedEntered);
		leaveRequest.setDateModified(new Date());
		if(!baseService.saveOrUpdate(leaveRequest)){
			beanForm.setMessage("Can not update time approval, please try later!");
			return mapping.findForward("leaveSummaryUpdate");
		} 
		response.getWriter().write(
				 "<script language=\"javascript\">\n" +
						" parent.dw.close();" + 
						" parent.document.forms[0].submit(); \n" +
				 "</script>");
		return null;
	}
	
	
}
